/**
 * GeneralSystemMessage.java
 * ************************** 
 * @date Apr 18, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.APIMessages;

import infrastructureAPI.Requester;
import infrastructureAPI.algorithms.APIFunction;
import messages.GeneralMessage;
import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import gossipServices.basic.nodeDescriptors.NodeDescriptor;

/**
 * Description: 
 * This class implements the RequestMessage interface, and it's
 * an extension of the GeneralMessage class. 
 * It contains only the basic attributes needed by a RequestMessage:
 * the APIFunction and the Requester.
 *
 */
public abstract class GeneralRequestMessage extends GeneralMessage
	implements RequestMessage{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected APIFunction apiFunction;
	protected Requester requester;
	

	/**
	 * 
	 * @param sender
	 * @param dest
	 * @param apiFunction
	 * @param requester
	 */
	public GeneralRequestMessage(NodeDescriptor sender, NodeDescriptor dest,
			APIFunction apiFunction, Requester requester) {
		super(sender, dest, null);
		setApiFunction(apiFunction);
		setRequester(requester);
	}
	
	/**
	 * 
	 * @param dest TODO
	 * @param apiFunction
	 * @param requester
	 */
	public GeneralRequestMessage(NodeDescriptor dest,APIFunction apiFunction, Requester requester){
		super( new ConcreteNodeDescriptor(requester.getRegisteredService(),
				requester.getNodeAddress(),
				requester.getPort()),
				dest, null);
		setApiFunction(apiFunction);
		setRequester(requester);
	}

	/**
	 * @param requester2
	 */
	protected void setRequester(Requester requester) {
		this.requester = requester;
	}

	protected void setApiFunction(APIFunction apiFunction){
		this.apiFunction = apiFunction;
	}
	
	/**
	 * 
	 */
	public GeneralRequestMessage() {}
	
	@Override
	public APIFunction getFunction() {
		return apiFunction;
	}
	
	public String toString(){
		String ret = new String("");
		ret += "RequestMessage:{";
		ret += super.toString()+"\n";
		ret += " sender:"+sender+"\n";
		if(apiFunction != null){
			ret += " request: "+apiFunction.getClass().getSimpleName();
		}
		ret+="}";
		return ret;
		
	}

	@Override
	public Requester replyTo() {
		return requester;
	}

}
